package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Notices;
import ex.com.jm.food.service.AdminNoticeService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;
/**
 * 通知action
 * @author zhaojw
 *
 */
public class AdminNoticeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private Notices notice;
	private AdminNoticeService adminNoticeService;
	private String noticeId;
	private String noticeTitle;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String s_date;
	private String e_date;
	private Map<String, String> dateMap;
	public Notices getNotice() {
		return notice;
	}
	public void setNotice(Notices notice) {
		this.notice = notice;
	}
	public AdminNoticeService getAdminNoticeService() {
		return adminNoticeService;
	}
	public void setAdminNoticeService(AdminNoticeService adminNoticeService) {
		this.adminNoticeService = adminNoticeService;
	}
	public String getNoticeId() {
		return noticeId;
	}
	public void setNoticeId(String noticeId) {
		this.noticeId = noticeId;
	}
	public String getNoticeTitle() {
		return noticeTitle;
	}
	public void setNoticeTitle(String noticeTitle) {
		this.noticeTitle = noticeTitle;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	/**
	 * 到达列表页面
	 * @return
	 */
	public String noticeList(){
		dateMap=DateUtils.getDate();
		return "noticeList";
	}
	/**
	 * 得到数据返回json
	 */
	public void getNotices(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		System.out.println(s_date+"====>>>"+e_date);
		 List<Notices>	notices = this.getAdminNoticeService().getNotices(offset, pageRow, noticeTitle, s_date, e_date);
		 totalCount=this.getAdminNoticeService().noticeCount(noticeTitle, s_date, e_date);
		JSONArray jsonnotice=JSONArray.fromObject(notices);
		JSONObject jo=JsonUtils.buildJson(jsonnotice, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out=response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	
	/**
	 * 编辑
	 * @return
	 */
	public String editNotice(){
		if (noticeId!=null) {
			notice=this.getAdminNoticeService().editNotices(noticeId);
		}
		return "edit";
	}
	/**
	 * 保存或者更新公告
	 * @return
	 */
	public String saveNotice(){
		if(notice.getNoticeTitle()==null||notice.getNoticeTitle().equals("")){
			addFieldError("notice.noticeTitle", "公告标题不能为空！");
			return "edit";
		}
		if(notice.getNoticeId()!=null&&!notice.getNoticeId().equals("")){
			this.getAdminNoticeService().updateNotice(notice);
			return "noticeList";
		}
		this.getAdminNoticeService().saveNotice(notice);
		return "noticeList";
	}
	/**
	 * 删除公告
	 * @return
	 */
	public String delNotice(){
		this.getAdminNoticeService().delNotice(noticeId);
		return "noticeList";
		
	}
}
